package com.example.pickupclosefirstdemo.main;

import android.animation.ObjectAnimator;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModelProviders;

import com.alibaba.android.arouter.facade.Postcard;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.facade.callback.NavigationCallback;
import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.rxbus.RxBus;
import com.example.pickupclosefirstdemo.R;
import com.example.pickupclosefirstdemo.adapter.InterflowAdapter;
import com.example.pickupclosefirstdemo.application.MVApplication;
import com.example.pickupclosefirstdemo.aroutersecond.ArouterSecondActivity;
import com.example.pickupclosefirstdemo.base.BaseActivity;
import com.example.pickupclosefirstdemo.bean.Constants;
import com.example.pickupclosefirstdemo.cornermark.BadgeNumberManager;
import com.example.pickupclosefirstdemo.databinding.ActivityMainBinding;
import com.example.pickupclosefirstdemo.fragmentinterflowactivity.InterflowActivity;
import com.example.pickupclosefirstdemo.interflowactivity.InterflowSecondActivity;
import com.example.pickupclosefirstdemo.util.LogUtil;

import static com.example.pickupclosefirstdemo.bean.Constants.AROUTER_ACT_TO_AROUTERSECONDACTIVITY;
import static com.example.pickupclosefirstdemo.bean.Constants.AROUTER_ACT_TO_INTERFLOWACTIVITY;
import static com.example.pickupclosefirstdemo.bean.Constants.AROUTER_ACT_TO_MAINACTIVITY;
import static com.example.pickupclosefirstdemo.bean.Constants.AROUTER_INTENT_INT;

@Route(path = AROUTER_ACT_TO_MAINACTIVITY)
public class MainActivity extends BaseActivity<ActivityMainBinding> {

    TextView tvModuleResult;
    Button btnModuleAdd, btnModuleDelete;

    PickUpViewModule vm;
    MutableLiveData<Integer> number;

    Teacher teacher;

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return  R.layout.activity_main;
    }

    @Override
    public void initView() {
        binding.setActivity(this);

        binding.btnModule.setText("阿诺斯");

        tvModuleResult = findViewById(R.id.tvModuleResult);
        btnModuleAdd = findViewById(R.id.btnModuleAdd);
        btnModuleDelete = findViewById(R.id.btnModuleDelete);

        //***************************dataBinding雙向綁定******************************************
        //第一种，使用PickUpViewModule（MutableLiveData）
        //        vm = ViewModelProviders.of(this).get(PickUpViewModule.class);
        //        binding.setViewModule(vm);
        //        MutableLiveData<String> str = vm.getNumber();
        //        str.setValue("99");
        //        str.observe(this, new Observer<String>() {
        //            @Override
        //            public void onChanged(String s) {
        //                tvModuleResult.setText(s);
        //            }
        //        });

        //第二种,使用User（BaseObservable）
        //        User user = new User("娜娜", "19");
        //        binding.setUser(user);

        //第三种,使用Teacher(ObservableField,ObservableChar,ObservableInt,ObservableLong,ObservableDoule……）
        teacher = ViewModelProviders.of(this).get(Teacher.class);
        teacher.teacherName.set("露娜");
        binding.setTeacher(teacher);


        //***************************輸入框輸入，界面上移******************************************
        controlKeyboardLayout(binding.llModuleParesent, binding.llModuleSon);
        binding.etModule.setInputType(129);


        //****************************華爲/小米/OPPO/VIVO 桌面角標*********************************
        BadgeNumberManager.from(MVApplication.getInstance()).setBadgeNumber(10);
        //取消的時候可以設置0 ，不過一般都是運行的時候自動取消


        //****************************ARouter用法**************************************************



        //****************************RxBus用法*********************************
        //粘性   不设置TAG直接发送对应类型值
        //        RxBus.getDefault().postSticky("冰灵");

        RxBus.getDefault().subscribeSticky(this, new RxBus.Callback<String>() {
            @Override
            public void onEvent(String o) {
                LogUtil.e("粘性RxBus-->" + o);
            }
        });

        //粘性   设置TAG直接发送对应类型值
        RxBus.getDefault().postSticky("冰雪", "nana");
        RxBus.getDefault().subscribeSticky(this, "nana", new RxBus.Callback<String>() {
            @Override
            public void onEvent(String s) {
                LogUtil.e("粘性RxBus-->" + s);
            }
        });

        //非粘性  不设置TAG直接接收对应类型值
        RxBus.getDefault().subscribe(this, new RxBus.Callback<String>() {
            @Override
            public void onEvent(String s) {
                LogUtil.e("RxBus-->" + s);
            }
        });

        //非粘性  设置TAG接收对应值
        RxBus.getDefault().subscribe(this, "ai", new RxBus.Callback<String>() {
            @Override
            public void onEvent(String s) {
                LogUtil.e("RxBus-->" + s);
            }
        });

    }

    @Override
    public void initData() {

    }

    int srollHeight, recordVisibleRec;

    private void controlKeyboardLayout(final View root, final View scrollToView) {
        root.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                //獲取當前界面的高度    虛擬鍵盤的頂點位置也就是當前界面的底部高度
                Rect rect = new Rect();
                root.getWindowVisibleDisplayFrame(rect);
                int rootInvisibleHeight = root.getRootView().getHeight() - rect.bottom;
                if (Math.abs(rootInvisibleHeight - recordVisibleRec) > 200) {
                    if (rootInvisibleHeight > 200) {
                        int[] location = new int[2];
                        scrollToView.getLocationInWindow(location);
                        srollHeight = (location[1] + scrollToView.getHeight()) - rect.bottom;
                        srollHeight = 0 - (srollHeight / 4);
                        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(scrollToView, "translationY", 0f, srollHeight);
                        objectAnimator.setDuration(500);
                        objectAnimator.start();
                    } else {
                        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(scrollToView, "translationY", srollHeight, 0f);
                        objectAnimator.setDuration(500);
                        objectAnimator.start();
                    }
                }
                recordVisibleRec = rootInvisibleHeight;
            }
        });
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case AROUTER_INTENT_INT:
                LogUtil.e("接受到返回值-->" + requestCode);
                LogUtil.e("接收返回值-->"+ data.getStringExtra("module_result"));
                break;
            default:
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        RxBus.getDefault().unregister(this);
    }

    public void mainClick(View view) {
        switch (view.getId()) {
            case R.id.btnModule:
                RxBus.getDefault().post("岁月静好", "ai");
                break;
            case R.id.btnModuleArouter:
                //fragment调用Activity界面方法
                Intent intent = new Intent(MainActivity.this, InterflowSecondActivity.class);
                startActivityForResult(intent, AROUTER_INTENT_INT);
                //                ARouter.getInstance().build(AROUTER_ACT_TO_INTERFLOWACTIVITY)
                //                        .withString("key","青儿")
                //                        .navigation(MainActivity.this, new NavigationCallback() {
                //                            @Override
                //                            public void onFound(Postcard postcard) {
                //                                LogUtil.e("路由目标发现");
                //                            }
                //
                //                            @Override
                //                            public void onLost(Postcard postcard) {
                //                                LogUtil.e("路由目标丢失");
                //                            }
                //
                //                            @Override
                //                            public void onArrival(Postcard postcard) {
                //                                LogUtil.e("路由目标跳转");
                //                            }
                //
                //                            @Override
                //                            public void onInterrupt(Postcard postcard) {
                //                                LogUtil.e("路由目标被拦截");
                //                            }
                //                        });
                break;
            default:
        }
    }

}
